﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BattleUIEvent : MonoBehaviour 
{
    public Transform settingWindow;
    public Transform inventoryWindow;
    public UIPopupList controlList;
    public UIPopupList inputList;

    void Start () {
        
    }
    
    public void OnClickSetting()
    {
        settingWindow.gameObject.SetActive(true);
        Time.timeScale = 0;
    }

    public void GetCurrentInputSelection ()
    {
        if (inputList != null)
        {
            string text = inputList.isLocalized ?
                Localization.Get(inputList.value) :
                    inputList.value;

            switch (text)
            {
            case "Swipe&Tap":
                InGameSetting.inputMode = InputType.FINGERTAPSWIPE;
                //Misc.drawLineMode = true;
                InputUIManager.instance.DisabeInputUI();
                break;
                /*
            case "VirtualControl":
                InGameSetting.inputMode = InputType.VIRTUALCONTROL;
                Misc.drawLineMode = false;
                InputUIManager.instance.EnableInputUI();
                break;
                */
            case "Joystick":
                InGameSetting.inputMode = InputType.VIRTUALJOYSTICK;
                //Misc.drawLineMode = false;
                InputUIManager.instance.EnableInputUI();
                break;
            }
        }
    }

    public void CloseSettingPanel()
    {
        settingWindow.gameObject.SetActive(false);
        Time.timeScale = 1;
    }

    public void QuitGame()
    {
        Application.Quit();
    }

    public float battlePressDuration = 1.0f;
    public float battlePressTime;
    public void OnClickBattleButton()
    {
        ManuelController.instance.AddCommand(ButtonType.Attack);
    }
    public void OnPressBattleButton()
    {
        Debug.LogError("OnPressBattleButton");
        battlePressTime = Time.time;
    }

    public UIButton attackButton;

    public void OnReleaseBattleButton()
    {
        if(InGameSetting.activeLongPressBtn)
        {
            if(Time.time - battlePressTime > battlePressDuration)
            {
                Debug.LogError("Long press Battle!");
                //do powerful attack

                RecoverEvent(attackButton);
            }
        }
    }

    //Evade
    public void OnClickSpecial1Button()
    {
        if(InGameSetting.inputMode == InputType.VIRTUALJOYSTICK)
        {
            ManuelController.instance.AddCommand(ButtonType.Evade);
        }
    }
    public void OnPressSpecial1Button()
    {
        Debug.LogError("OnPressSpecial1Button");
    }
    public void OnReleaseSpecial1Button()
    {
        Debug.LogError("OnReleaseSpecial1Button");
    }
    
    public UIButton weaponSpecialButton;
    public UISprite weaponSpecialButtonMask;
    public float weaponSpecialFillSpeed = 0.1f;
    public void OnClickSpecial2Button()
    {
        Debug.LogError("OnClickSpecial2Button");
        if(!weaponSpecialButton.isEnabled) return;
        if(InGameSetting.inputMode == InputType.VIRTUALJOYSTICK)
        {
            ManuelController.instance.AddCommand(ButtonType.WeaponSpecial);
        }
        else if(InGameSetting.inputMode == InputType.FINGERTAPSWIPE)
        {
            TouchScreenController.instance.WeaponSkillInput();
        }
        weaponSpecialButton.isEnabled = false;
        weaponSpecialButtonMask.fillAmount = 1.0f;
        StartCoroutine(UIMask(weaponSpecialButtonMask,weaponSpecialButton,weaponSpecialFillSpeed));
    }

    //Kuso
    public float weaponEffectPressDuration = 1.0f;
    public float weaponEffectTime;

    public void OnPressSpecial2Button()
    {
        weaponEffectTime = Time.time;
    }
    public void OnReleaseSpecial2Button()
    {
        if(InGameSetting.activeLongPressBtn)
        {
            if(Time.time - weaponEffectTime > weaponEffectPressDuration)
            {
                Debug.LogError("Long press WeaponEffect! ");
                // do powerful weapon effect


                StartCoroutine(RecoverEvent(weaponSpecialButton));
            }
        }
    }

    IEnumerator RecoverEvent(UIButton button)
    {
        EventDelegate d = button.onClick[0];
        button.onClick.Clear();
        yield return new WaitForEndOfFrame();
        button.onClick.Add(d);
    }

    public UIButton waveAttackButton;
    public UISprite waveAttackButtonMask;
    public float waveAttackFillSpeed = 0.1f;
    public void OnClickSpecial3Button()
    {
        if(!waveAttackButton.isEnabled) return;
        if(InGameSetting.inputMode == InputType.VIRTUALJOYSTICK)
        {
            ManuelController.instance.AddCommand(ButtonType.WaveSpecial);
        }
        waveAttackButton.isEnabled = false;
        waveAttackButtonMask.fillAmount = 1.0f;
        StartCoroutine(UIMask(waveAttackButtonMask,waveAttackButton,waveAttackFillSpeed));
        Debug.LogError("OnClickSpecial3Button");
    }

    public float waveAttackPressDuration = 1.0f;
    public float waveAttackTime;

    public void OnPressSpecial3Button()
    {
        waveAttackTime = Time.time;
    }
    public void OnReleaseSpecial3Button()
    {
        if(InGameSetting.activeLongPressBtn)
        {
            if(Time.time - waveAttackTime > waveAttackPressDuration)
            {
                Debug.LogError("Long press waveAttack! ");
                // do powerful wave attack

                StartCoroutine(RecoverEvent(waveAttackButton));

            }
        }
    }

    IEnumerator UIMask(UISprite buttonMask,UIButton button, float fillSpeed)
    {
        while(buttonMask.fillAmount > 0.0f)
        {
            buttonMask.fillAmount -= fillSpeed * Time.deltaTime;
            yield return null;
        }
        yield return null;
        buttonMask.fillAmount = 0.0f;
        button.isEnabled = true;
    }

    public UIToggle bloodToggle;
    public void OnBloodToggleValueChange()
    {
        InGameSetting.bloodEffect = bloodToggle.value;
        Debug.Log(InGameSetting.bloodEffect);
    }

    public UIToggle vibrationToggle;
    public void OnVibrationToggleValueChange()
    {
        InGameSetting.vibration = vibrationToggle.value;
    }

    public UIToggle touchToggle;
    public void OnTouchValueChange()
    {
        if(touchToggle.value)
        {
            InGameSetting.inputMode = InputType.FINGERTAPSWIPE; 
        }
        else
        {
            InGameSetting.inputMode = InputType.VIRTUALJOYSTICK; 
        }
        GameUtils.SetControl();
        DPadToggle.value = !touchToggle.value;
        if(touchToggle.value)
        {
            InputUIManager.instance.SetUIStatus(false);
        }
        else
        {
            InputUIManager.instance.SetUIStatus(true);
        }
    }

    public UIToggle DPadToggle;
    public void OnDPadValueChange()
    {
        if(DPadToggle.value)
        {
            InGameSetting.inputMode = InputType.VIRTUALJOYSTICK; 
        }
        else
        {
            InGameSetting.inputMode = InputType.FINGERTAPSWIPE; 
        }
        GameUtils.SetControl();
        touchToggle.value = !DPadToggle.value;
        if(DPadToggle.value)
        {
            InputUIManager.instance.SetUIStatus(true);
        }
        else
        {
            InputUIManager.instance.SetUIStatus(false);
        }
    }

    public UISlider volumeBar;
    public void OnVolumeValueChange()
    {
        InGameSetting.volume = volumeBar.value;
        FXSoundManager.instance.ChangeVolume(volumeBar.value);
    }
    
}
